﻿using System.Globalization;
using RazakiBLL;
using System;
using RazakiBLL.Managers;

public static class UtilitiesBLL
{

    #region >>>CONSTANT<<<

    public const int _DECLINE_ID = -1;

    #endregion

    #region >>>EIELD<<<<

    private static readonly string _getPersianDate = string.Empty;

    private static readonly string _getGregorianDate = string.Empty;

    #endregion

    #region >>>METHOD<<<

    public static ICacheProvider CacheProvider { get; set; }

    public static string GetUserUniqueKey()
    {
        var key = CacheProvider.GetData<string>("UserUniqueKey");

        if (key == null)
        {
            key = GenerateGuid();
            CacheProvider.SetData("UserUniqueKey", key);
        }

        return key;
    }

    public static string GenerateGuid()
    {
        //old return Guid.NewGuid().ToString().Replace("-", String.Empty);
        //.ToString("n") or .ToString("N")
        return Guid.NewGuid().ToString("n");
    }

    public static string ShortText(string text, int maxLength)
    {
        string result = text;

        if (text.Length > maxLength)
            result = String.Format("{0}...", text.Substring(0, maxLength));

        return result;
    }

    public static string GetPersianDate(object dateTime, bool withTime = false)
    {
        lock (_getPersianDate)
        {
            string result;
            DateTime targetDateTime = DateTime.Parse(dateTime.ToString());
            PersianCalendar persianCalendar = new PersianCalendar();

            result = String.Format("{0:0000}/{1:00}/{2:00}", persianCalendar.GetYear(targetDateTime),
                                   persianCalendar.GetMonth(targetDateTime),
                                   persianCalendar.GetDayOfMonth(targetDateTime));
            if (withTime)
                result = String.Format("{0} {1:00}:{2:00}", result, targetDateTime.Hour, targetDateTime.Minute);

            return result;
        }
    }

    /// <summary>
    /// e.g.: 1390/12/18
    /// </summary>
    /// <param name="jalali"></param>
    /// <returns></returns>
    public static DateTime? GetGregorianDate(string jalali)
    {
        lock (_getGregorianDate)
        {
            try
            {
                string[] jalaliParts = jalali.Split('/');
                int year = int.Parse(jalaliParts[0]), month = int.Parse(jalaliParts[1]), day = int.Parse(jalaliParts[2]);
                PersianCalendar persianCalendar = new PersianCalendar();
                DateTime result;

                result = persianCalendar.ToDateTime(year, month, day, 0, 0, 0, 0);

                return result;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }

    public static string GetVersion()
    {
        string usualVersion = "2.0";
        string result;
        UowRazaki uowRazaki = new UowRazaki();

#if DEBUG
        //add or edit
        result = UtilitiesBLL.GetPersianDate(DateTime.Now).Replace("/", ".").Remove(0, 2);
        uowRazaki.ApplicationPropertyRepository.AddEditByPropertyName(EPropertyNames.version.ToString(), result);
        uowRazaki.Save();
#else
        //get
        result = UtilitiesBLL.CacheProvider.GetData<string>("Version");
        if (result == null)
        {
            result = string.Format("{0}", uowRazaki.ApplicationPropertyRepository.GetByPropertyName(EPropertyNames.version.ToString()));
            UtilitiesBLL.CacheProvider.SetData("Version", result);
        }
#endif

        return string.Format("{0}-{1}", result, usualVersion);
    }

    public static string SeperateDigits(long num)
    {
        return num.ToString("N0");
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="num"></param>
    /// <param name="isDecimal">اگر منفی است ممیز خنثی شود</param>
    /// <returns></returns>
    public static string RawNumber(string num, bool isDecimal = false)
    {
        string result;

        result = num.Replace(",", string.Empty);
        if (!isDecimal && result.IndexOf(".") > 0)
            result = result.Remove(result.IndexOf("."));

        return result;
    }

    #endregion

}